package cn.lili.controller;

import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.system.client.UrbanVipCodeClient;
import cn.lili.modules.system.entity.dos.LiVipActiveCode;
import cn.lili.modules.system.entity.dos.LiVipApply;
import cn.lili.modules.system.entity.dto.ApplyCodeDto;
import cn.lili.modules.system.entity.dto.VipCodeDto;
import cn.lili.modules.urbansubstation.client.UrbanSubstationClient;
import cn.lili.modules.urbansubstation.entity.dos.UrbanSubstation;
import cn.lili.modules.urbansubstation.entity.dto.VipCodeSearchPageParams;
import cn.lili.modules.urbansubstation.entity.dto.VipCodeSearchParams;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/urban-substation/vipCode")
@Api(tags = "城市站点激活码管理")
public class UrbanVipCodeController {
    @Autowired
    private UrbanVipCodeClient vipCodeClient;
    @Autowired
    private UrbanSubstationClient urbanSubstationClient;

    /**
     * 前端传的城市站点id是城市站点库表的userId，user表的id
     * @param dto
     * @return
     */
    @ApiModelProperty("激活码申请列表")
    @PostMapping("/list")
    public ResultMessage<Object> list(@RequestBody ApplyCodeDto dto){
        AuthUser authUser = UserContext.getCurrentUser();
        dto.setSupplierId(authUser.getId());

        return ResultUtil.data(vipCodeClient.list(dto));
    }

    @ApiModelProperty("申请激活码")
    @PostMapping("/codeApply")
    public ResultMessage<Object> codeApply(@RequestBody LiVipApply liVipApply){
        AuthUser authUser = UserContext.getCurrentUser();
        liVipApply.setApplySupplierId(authUser.getId());
        UrbanSubstation urbanSubstation = urbanSubstationClient.getUrbanSubstation(authUser.getId());
        liVipApply.setApplySupplierName(urbanSubstation.getUrbanName());

        vipCodeClient.codeApply(liVipApply);
        return ResultUtil.success();
    }

    @ApiModelProperty("获取城市分站激活码列表")
    @GetMapping("/getCodePage")
    public ResultMessage<Page<LiVipActiveCode>> getCodePage (VipCodeSearchParams vipCodeSearchParams, PageVO page) {
        AuthUser currentUser = UserContext.getCurrentUser();
        vipCodeSearchParams.setUrbanId(currentUser.getId());

        VipCodeSearchPageParams vipCodeSearchPageParams = new VipCodeSearchPageParams();
        vipCodeSearchPageParams.setVipCodeSearchParams(vipCodeSearchParams);
        vipCodeSearchPageParams.setPage(page);
        Page<LiVipActiveCode> vipActiveCodePage = vipCodeClient.getPageByUrbanId(vipCodeSearchPageParams);
        return ResultUtil.data(vipActiveCodePage);
    }
}
